#coding=utf-8

#A股代码
#page: 'http://www.sse.com.cn/sseportal/webapp/datapresent/SSEQueryStockInfoAct?keyword=&reportName=BizCompStockInfoRpt&PRODUCTID=&PRODUCTJP=&PRODUCTNAME=&CURSOR=1'

import utils
def get_scode_from_page(soup):
    
    def parse_row(row):
        code = row.contents[1].contents[0].string
        code = code.encode('u8')
        name = row.contents[3].string 
        name = name.encode('u8')
        return [code, name]
        
    result = []
    #soup = utils.get_soup(url)
    t = unicode('证券代码', 'u8')
    row = soup.find(text=t).parent.parent    
    while True:
        row =  row.nextSibling.nextSibling 
        if not row: break
        data = parse_row(row)
        result.append(data)
    return result

def get_next50_url(soup):
    #soup = utils.get_soup(url)
    
    nextp = unicode('下一页', 'u8')
    r = soup.find(text=nextp)
    if not r:return None
    
    n50_url= r.parent['href']
    n50_url = 'http://www.sse.com.cn' + n50_url      
    return n50_url

def get_scode_from_sse():
    cn = []
    url = 'http://www.sse.com.cn/sseportal/webapp/datapresent/SSEQueryStockInfoAct?keyword=&reportName=BizCompStockInfoRpt&PRODUCTID=&PRODUCTJP=&PRODUCTNAME=&CURSOR=1'
    while 1:
        if not url:return cn
        
        soup = utils.get_soup(url)
        cn = cn + get_scode_from_page(soup)
        url = get_next50_url(soup)
        
if __name__ == '__main__':
    cn = get_scode_from_sse()
    #print cn, '\n'
    print len(cn)
